<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button @click="handleAdd" icon="plus" style="margin-left: 8px" v-if="!formDisabled">新增</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <span slot="jkxsRender" slot-scope="text, record">
          {{ getJkxsText(record.jkxs) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template v-if="!formDisabled">
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
              <a>删除</a>
            </a-popconfirm>
          </template>
        </span>
      </a-table>
    </div>

    <gc-project-entry-section-modal ref="modalForm" @ok="modalFormOk"></gc-project-entry-section-modal>
  </a-card>
</template>

<script>
import { initDictOptions, filterDictText } from '@/components/dict/JDictSelectUtil'
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import GcProjectEntrySectionModal from './EntrySectionModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { getAction } from '@/api/manage'
export default {
  name: 'GcProjectEntrySectionList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    GcProjectEntrySectionModal,
  },
  props: {
    entryId: {
      type: String,
      default: '',
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      description: '项目标段信息管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        // {
        //   title: 'id',
        //   align: 'center',
        //   dataIndex: 'id',
        // },
        {
          title: '标段名称',
          align: 'center',
          dataIndex: 'sectionName',
        },
        {
          title: '标段编号',
          align: 'center',
          dataIndex: 'sectionCode',
        },
        {
          title: '价款形式',
          align: 'center',
          dataIndex: 'jkxs',
          customRender: (text, record, index) => {
            //字典值替换通用方法
            return filterDictText(this.jkxsOptions, text)
          },
        },
        {
          title: '招标类别',
          align: 'center',
          dataIndex: 'zblb',
          customRender: (text, record, index) => {
            //字典值替换通用方法
            return filterDictText(this.zblbOptions, text)
          },
        },
        {
          title: '合同估算价(万元)',
          align: 'center',
          dataIndex: 'htgsj',
        },
        {
          title: '标段分类',
          align: 'center',
          dataIndex: 'bdType'
        }, 
        {
          title: '计划开工日期',
          align: 'center',
          dataIndex: 'jhkgrq',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '工程规模',
          align: 'center',
          dataIndex: 'gcgm',
        },
        {
          title: '工程概况',
          align: 'center',
          dataIndex: 'gcgk',
        },
        {
          title: '要求投标人资质等级',
          align: 'center',
          dataIndex: 'yqtbrzzdj',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        {
          title: '序号',
          align: 'center',
          dataIndex: 'sortNum',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/gcProject/gcProjectEntrySection/list',
        delete: '/gcProject/gcProjectEntrySection/delete',
        deleteBatch: '/gcProject/gcProjectEntrySection/deleteBatch',
      },
      dictOptions: {},
      superFieldList: [],
      queryParam: {
        entryId: '',
      },
      loading: false,
      jkxsOptions: [],
      zblbOptions: [],
      sectionTypeOptions: [],
    }
  },
  watch: {
    entryId(newVal) {
      this.queryParam.entryId = newVal
      this.loadData(1)
    },
  },
  created() {
    this.queryParam.entryId = this.entryId
    this.getSuperFieldList()
    this.initDictConfig()
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  methods: {
    initDictConfig() {
      //初始化字典
      initDictOptions('sys_price_type').then((res) => {
        if (res.success) {
          this.jkxsOptions = res.result
        }
      })
      //招标类别
      initDictOptions('sys_zb_type').then((res) => {
        if (res.success) {
          this.zblbOptions = res.result
        }
      })
    },
    sectionTypeText(value){
      return value +'aaa';
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'entryId', text: '进场表主键', dictCode: '' })
      fieldList.push({ type: 'string', value: 'sectionName', text: '标段名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'sectionCode', text: '标段编号', dictCode: '' })
      fieldList.push({
        type: 'string',
        value: 'jkxs',
        text: '价款形式(字典：sys_price_type)',
        dictCode: 'sys_price_type',
      })
      fieldList.push({ type: 'string', value: 'htgsj', text: '合同估算价', dictCode: '' })
      fieldList.push({
        type: 'string',
        value: 'sectionType',
        text: '标段分类代码(字典：sys_project_section_category)',
        dictCode: 'sys_project_section_category',
      })
      fieldList.push({ type: 'string', value: 'sortNum', text: '显示序号', dictCode: '' })
      fieldList.push({ type: 'date', value: 'jhkgrq', text: '计划开工日期' })
      fieldList.push({ type: 'string', value: 'gcgm', text: '工程规模', dictCode: '' })
      fieldList.push({ type: 'string', value: 'gcgk', text: '工程概况', dictCode: '' })
      fieldList.push({ type: 'date', value: 'yqtbrzzdj', text: '要求投标人资质等级' })
      fieldList.push({ type: 'int', value: 'version', text: '版本号', dictCode: '' })
      this.superFieldList = fieldList
    },
    modalFormOk(row) {
      var that = this
      if (!row.sortNum) {
        row.sortNum = that.dataSource.length + 1
      }
      var obj = Object.assign({}, row)
      var index = that.dataSource.findIndex((item) => {
        if (item.id == row.id) {
          return true
        }
      })
      if (index > -1) {
        this.dataSource.splice(index, 1)
      }
      this.dataSource.push(obj)
      this.dataSource.sort(function (a, b) {
        return a.sortNum - b.sortNum
      })
      this.$emit('ok')
    },
    handleDelete: function (_id) {
      var that = this
      var index = that.dataSource.findIndex((item) => {
        if (item.id == _id) {
          return true
        }
      })
      if (index > -1) {
        this.dataSource.splice(index, 1)
        this.$emit('ok')
      }
    },
    loadData(arg) {
      if (this.queryParam.entryId == '') {
        return
      }

      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params)
        .then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>